﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;
using System.Collections;
using System.Data;

namespace Web.Paper
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!((Account)Session["Account"]).RoleCurrent.Equals('t')) Response.Redirect("Default.aspx");
            //获取QueryString
           
            string stdid = Request.QueryString["student"].ToString();
            int docstyle =int.Parse(Request.QueryString["style"].ToString());

            Student myStudent = new Student(stdid);
            PaperFile myPaperFile = new PaperFile(docstyle, stdid, ((Teacher)Session["Teacher"]).TeacherId);

            Label_stuPaperTitle2.Text = myStudent.Papers.PaperTitle;
            //获得某类型所有文档
            DataSet ds = myPaperFile.GetConversationFile();
            if (ds.Tables[0].Rows.Count == 0)
                Label_NoData.Text = "该学生没有提交文档";
            GridView_DocFile.DataSource = ds;
            GridView_DocFile.DataBind();


            GMS.BIZ.Paper myPaper=new GMS.BIZ.Paper(stdid,myStudent.GraduateYear);
            //获取某类型单个文档的文档状态(1,2,3,4)
            int paperstatus=myPaper.GetOneStatus(docstyle);

            //绑定DropDownList
            if (!IsPostBack)
            {
                ArrayList arraylist = new ArrayList();
                arraylist = Library.GetDocumentStatusLib();
                for (int i = 0; i < arraylist.Count; i++)
                {
                    ListItem myListItem = new ListItem();
                    myListItem = (ListItem)arraylist[i];
                    DropDownList_DocStatus.Items.Add(myListItem);
                }

                DropDownList_DocStatus.SelectedIndex = paperstatus;
            }
            //绑定已经通过的论文名，非通过则为NULL
            GMS.BIZ.PaperInfo myPaperInfo=new PaperInfo();
            myPaperInfo=myPaper.GetPaperInfo();
            if (paperstatus == 1)
            {
                switch (docstyle)
                {
                    case 1:
                        decimal doc1id = (decimal)myPaperInfo.Doc1file;
                        Label_AccessPaperID.Text = PaperFile.GetFileName(doc1id); break;
                    case 2:
                        decimal doc2id = (decimal)myPaperInfo.Doc2file;
                        Label_AccessPaperID.Text = PaperFile.GetFileName(doc2id); break;
                    case 3:
                        decimal doc3id = (decimal)myPaperInfo.Doc3file;
                        Label_AccessPaperID.Text = PaperFile.GetFileName(doc3id); break;
                    case 4:
                        decimal doc4id = (decimal)myPaperInfo.Doc4file;
                        Label_AccessPaperID.Text = PaperFile.GetFileName(doc4id); break;
                    case 5:
                        decimal doc5id = (decimal)myPaperInfo.Doc5file;
                        Label_AccessPaperID.Text = PaperFile.GetFileName(doc5id); break;
                    case 6:
                        decimal doc6id = (decimal)myPaperInfo.Doc6file;
                        Label_AccessPaperID.Text = PaperFile.GetFileName(doc6id); break;
                    default: break;
                }
            }
            else Label_AccessPaperID.Text = "没有通过的论文";
            
            //for (int i = 0; i < GridView_DocFile.Rows.Count; i++)
            //{
            //    if (GridView_DocFile.Rows[i].Cells[0].Text.CompareTo("1") == 0)
            //        Label_AccessPaperID.Text = GridView_DocFile.Rows[i].Cells[1].Text;
            //    else Label_AccessPaperID.Text = null;
            //}
            //指示GridView为哪种文档类型
            switch (docstyle)
            {
                case 1: Label_DocFile.Text = Resources.Docs.Doc1 + "列表："; break;
                case 2: Label_DocFile.Text = Resources.Docs.Doc2 + "列表："; break;
                case 3: Label_DocFile.Text = Resources.Docs.Doc3 + "列表："; break;
                case 4: Label_DocFile.Text = Resources.Docs.Doc4 + "列表："; break;
                case 5: Label_DocFile.Text = Resources.Docs.Doc5 + "列表："; break;
                case 6: Label_DocFile.Text = Resources.Docs.Doc6 + "列表："; break;
                default: break;
            }
        }
        
        //上传回复文件按钮
        protected void ImageButton_upload_Click(object sender, ImageClickEventArgs e)
        {
            
            if (FileUpload1.PostedFile.ContentLength == 0)
            {
                Label_NoUploadFile.Text = "请选择上传文件";
                Label1.Visible = false;
            }
            //获取QueryString
            else
            {
                string stdid = Request.QueryString["student"].ToString();
                int docstyle = int.Parse(Request.QueryString["style"].ToString());

                System.IO.Stream myStream;
                myStream = FileUpload1.FileContent;

                string filename = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1);
                string type = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("."));
                string gettype = System.IO.Path.GetExtension(FileUpload1.FileName);
                if ((gettype == ".rar") || (gettype == ".zip") || (gettype == ".doc"))
                {
                    Student myStudent = new Student(stdid);

                    GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(stdid, myStudent.GraduateYear);
                    PaperFile myPaperFile = new PaperFile(docstyle, stdid, myPaper.TeacherID);
                    string ok = myPaperFile.UploadFile(TextBox_comment.Text.ToString(), ((Account)Session["Account"]).RoleCurrent.ToString(), myStream, type);
                    if (ok.CompareTo(Resources.ErrorMessage.OK) == 0)
                        Label_result.Text = Resources.ErrorMessage.OK;
                    else Label_result.Text = Resources.ErrorMessage.DBOptError;
                    Label_comment.Text = "";
                    Label_NoUploadFile.Text = "";
                    Label1.Visible = false;
                }
                else
                    Label1.Visible = true;
            }
        }
        
        //下载某个文档按钮
        protected void ImageButton_download_Click(object sender, ImageClickEventArgs e)
        {
            string stdid = Request.QueryString["student"].ToString();
            int docstyle = int.Parse(Request.QueryString["style"].ToString());
            Student myStudent = new Student(stdid);


            GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(stdid, myStudent.GraduateYear);
            PaperFile myPaperFile = new PaperFile(docstyle, stdid, myPaper.TeacherID);
            myPaperFile.GetOneFile(decimal.Parse(((ImageButton)sender).CommandArgument.ToString()));
        }
        
        //修改文档状态按钮
        protected void ImageButton_ChangePaperStatus_Click(object sender, ImageClickEventArgs e)
        {
            string stdid = Request.QueryString["student"].ToString();
            int docstyle = int.Parse(Request.QueryString["style"].ToString());
            Student myStudent = new Student(stdid);

            GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(stdid, myStudent.GraduateYear);
            PaperFile myPaperFile = new PaperFile(docstyle, stdid, myPaper.TeacherID);
            //使用decimal变量给decimal?变量赋值就可以把decimal变量换成decimal?类型
            if (Label_DocFileID.Text != "")
            {
                decimal? abc = decimal.Parse(Label_DocFileID.Text.ToString());
                string str = myPaperFile.ChangeStatus(int.Parse(DropDownList_DocStatus.SelectedItem.Value.ToString()), abc);
                if (str.CompareTo(Resources.ErrorMessage.OK) == 0)
                    Label_Changeresult.Text = "论文状态已更改";
                else Label_Changeresult.Text = "状态修改未成功";
            }
        }

        //DropDownList选中项改变时发生
        protected void DropDownList_DocStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            Label_Changeresult.Text = "";
            if (DropDownList_DocStatus.SelectedValue.CompareTo("1") == 0)
            {
                string stdid = Request.QueryString["student"].ToString();
                int docstyle = int.Parse(Request.QueryString["style"].ToString());

                Student myStudent = new Student(stdid);
                PaperFile myPaperFile = new PaperFile(docstyle, stdid, ((Teacher)Session["Teacher"]).TeacherId);

                GridView_SelectDocFile.DataSource = myPaperFile.GetConversationFile();
                GridView_SelectDocFile.DataBind();
                GridView_SelectDocFile.Visible = true;
                ImageButton_ChangePaperStatus.Visible = false;
            }
            else
            {
                Label_DocFileID.Text = DropDownList_DocStatus.SelectedValue.ToString();
                GridView_SelectDocFile.Visible = false;
                ImageButton_ChangePaperStatus.Visible = true;
            }
        }

        protected void ImageButton_EditPaperStatus_Click(object sender, ImageClickEventArgs e)
        {
            string stdid = Request.QueryString["student"].ToString();
            int docstyle = int.Parse(Request.QueryString["style"].ToString());

            Student myStudent = new Student(stdid);
            PaperFile myPaperFile = new PaperFile(docstyle, stdid, ((Teacher)Session["Teacher"]).TeacherId);
            decimal? abc=decimal.Parse(((ImageButton)sender).CommandArgument.ToString());
            string str = myPaperFile.ChangeStatus(int.Parse(DropDownList_DocStatus.SelectedItem.Value.ToString()), abc);
            if (str == Resources.ErrorMessage.OK)
                Label_Changeresult.Text = str;
            else Label_Changeresult.Text = "状态修改未成功";
        }

        protected void ImageButton_readcomment_Click(object sender, ImageClickEventArgs e)
        {
           

            string stdid = Request.QueryString["student"].ToString();
            Student myStudent = new Student(stdid);
            int docstyle = int.Parse(Request.QueryString["style"].ToString());
            GMS.BIZ.Paper myPaper = new GMS.BIZ.Paper(stdid, myStudent.GraduateYear);
           
        }

        protected void GridView_DocFile_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_DocFile.PageIndex = e.NewPageIndex;
            string stdid = Request.QueryString["student"].ToString();
            int docstyle = int.Parse(Request.QueryString["style"].ToString());

            Student myStudent = new Student(stdid);
            PaperFile myPaperFile = new PaperFile(docstyle, stdid, ((Teacher)Session["Teacher"]).TeacherId);
            DataSet ds = myPaperFile.GetConversationFile();
            GridView_DocFile.DataSource = ds;
            GridView_DocFile.DataBind();

        }

        protected void GridView_SelectDocFile_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_SelectDocFile.PageIndex = e.NewPageIndex;
            string stdid = Request.QueryString["student"].ToString();
            int docstyle = int.Parse(Request.QueryString["style"].ToString());

            Student myStudent = new Student(stdid);
            PaperFile myPaperFile = new PaperFile(docstyle, stdid, ((Teacher)Session["Teacher"]).TeacherId);
            DataSet ds = myPaperFile.GetConversationFile();
            GridView_SelectDocFile.DataSource = ds;
            GridView_SelectDocFile.DataBind();
        }

       
    }
}
